package com.guigu.www.luolujin.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.luolujin.Service.LljMindTestService;
import com.guigu.www.mapper.*;
import com.guigu.www.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 作者 罗露金
 */
@Service
public class LljMindTestServiceImpl implements LljMindTestService {
    @Autowired
    MindTestMapper mindTestMapper;
    @Autowired
    RubricMapper rubricMapper;
    @Autowired
    RubricOptionMapper optionMapper;
    @Autowired
    UserAnswerMapper userAnswerMapper;
    @Autowired
    QuestionMapper questionMapper;

    @Override
    public PageVo<MindTest> PageMindTest(Integer pageNum, Integer pageSize, String mindTestName) {
        QueryWrapper<MindTest> wrapper = new QueryWrapper<>();
        wrapper.like("mind_test_name ",mindTestName);
        Page page = new Page(pageNum, pageSize);
        IPage iPage = mindTestMapper.selectPage(page, wrapper);
        PageVo<MindTest> pageVo = new PageVo<>();
        pageVo.setRows(iPage.getRecords());
        pageVo.setTotal(iPage.getTotal());
        return pageVo;
    }

    @Override
    public List<Rubric> selectRubric(Integer rubType) {
        List<Rubric> rubrics = rubricMapper.queryRubric(rubType);
        for (Rubric rubric : rubrics) {
            QueryWrapper<RubricOption> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("rub_id",rubric.getRubId());
            List<RubricOption> rubricOptions = optionMapper.selectList(queryWrapper);
            rubric.setOptions(rubricOptions);
        }
        return rubrics;
    }

    @Override
    public void insertUserAnswer(String memId, List<UserAnswer> UserAnswer) {
        for (UserAnswer userAnswer : UserAnswer) {
            QueryWrapper<UserAnswer> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("mem_id",memId);
            queryWrapper.eq("rub_id ",userAnswer.getRubId());
            userAnswerMapper.delete(queryWrapper);
            UserAnswer user = new UserAnswer();
            user.setMemId(Integer.parseInt(memId));
            user.setRubId(userAnswer.getRubId());
            user.setRubOpId(userAnswer.getRubOpId());
            userAnswerMapper.insert(user);

        }
    }

    @Override
    public void insertMindTest(String mindTestName, Integer memId) {
        QueryWrapper<UserAnswer> userAnswerQueryWrapper = new QueryWrapper<>();
        userAnswerQueryWrapper.eq("mem_id",memId);
        List<UserAnswer> userAnswers = userAnswerMapper.selectList(userAnswerQueryWrapper);
        Integer ph=0;
        Integer qx=0;
        Integer yx=0;
        Integer yxu=0;
        Integer ts=0;
        Integer sr=0;
        Integer xy=0;
        Integer qy=0;
        Integer tp=0;
        //合格分数
        float peh=0;
        float qix=0;
        float yanx=0;
        float yinxu=0;
        float tans=0;
        float shir=0;
        float xuey=0;
        float qiy=0;
        float tep=0;
        for (UserAnswer userAnswer : userAnswers) {
            //获取题目id
            Integer rubId = userAnswer.getRubId();
            //查询题库表 查询题目类型
            Rubric rubric = rubricMapper.selectById(rubId);
            Integer rubType = rubric.getRubType();
            if(rubType==10){
                //获取到及格分数
                QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ques_type ",2);
                queryWrapper.eq("ques_test_type ",10);
                queryWrapper.eq("ques_state ",0);
                Question question = questionMapper.selectOne(queryWrapper);
                float quesScore = question.getQuesScore();
                peh=quesScore;
                //获取每一个答题分数
                Integer rubOpId = userAnswer.getRubOpId();
                RubricOption rubricOption = optionMapper.selectById(rubOpId);
                if (rubricOption == null){
                    ph = 0;
                }else {
                    Integer rubOpScore = rubricOption.getRubOpScore();
                    ph=ph+rubOpScore;
                }
            }else if(rubType==11){
                QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ques_type ",2);
                queryWrapper.eq("ques_test_type ",11);
                queryWrapper.eq("ques_state ",0);
                Question question = questionMapper.selectOne(queryWrapper);
                float quesScore = question.getQuesScore();
                qix=quesScore;
                Integer rubOpId = userAnswer.getRubOpId();
                RubricOption rubricOption = optionMapper.selectById(rubOpId);
                if (rubricOption == null){
                    qx = 0;
                }else {
                    Integer rubOpScore = rubricOption.getRubOpScore();
                    qx=qx+rubOpScore;
                }
            }else if(rubType==12){
                QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ques_type ",2);
                queryWrapper.eq("ques_test_type ",12);
                queryWrapper.eq("ques_state ",0);
                Question question = questionMapper.selectOne(queryWrapper);
                float quesScore = question.getQuesScore();
                yanx=quesScore;
                Integer rubOpId = userAnswer.getRubOpId();
                RubricOption rubricOption = optionMapper.selectById(rubOpId);
                if (rubricOption == null){
                    yx = 0;
                }else {
                    Integer rubOpScore = rubricOption.getRubOpScore();
                    yx=yx+rubOpScore;
                }
            }else if(rubType==13){
                QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ques_type ",2);
                queryWrapper.eq("ques_test_type ",13);
                queryWrapper.eq("ques_state ",0);
                Question question = questionMapper.selectOne(queryWrapper);
                float quesScore = question.getQuesScore();
                yinxu=quesScore;
                Integer rubOpId = userAnswer.getRubOpId();
                RubricOption rubricOption = optionMapper.selectById(rubOpId);
                if (rubricOption == null){
                    yxu = 0;
                }else {
                    Integer rubOpScore = rubricOption.getRubOpScore();
                    yxu=yxu+rubOpScore;
                }
            }
            else if(rubType==14){
                QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ques_type ",2);
                queryWrapper.eq("ques_test_type ",14);
                queryWrapper.eq("ques_state ",0);
                Question question = questionMapper.selectOne(queryWrapper);
                float quesScore = question.getQuesScore();
                tans=quesScore;
                Integer rubOpId = userAnswer.getRubOpId();
                RubricOption rubricOption = optionMapper.selectById(rubOpId);
                if (rubricOption == null){
                    ts = 0;
                }else {
                    Integer rubOpScore = rubricOption.getRubOpScore();
                    ts=ts+rubOpScore;
                }
            }
            else if(rubType==15){
                QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ques_type ",2);
                queryWrapper.eq("ques_test_type ",15);
                queryWrapper.eq("ques_state ",0);
                Question question = questionMapper.selectOne(queryWrapper);
                float quesScore = question.getQuesScore();
                shir=quesScore;
                Integer rubOpId = userAnswer.getRubOpId();
                RubricOption rubricOption = optionMapper.selectById(rubOpId);
                if (rubricOption == null){
                    sr = 0;
                }else {
                    Integer rubOpScore = rubricOption.getRubOpScore();
                    sr=sr+rubOpScore;
                }
            }
            else if(rubType==16){
                QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ques_type ",2);
                queryWrapper.eq("ques_test_type ",16);
                queryWrapper.eq("ques_state ",0);
                Question question = questionMapper.selectOne(queryWrapper);
                float quesScore = question.getQuesScore();
                xuey=quesScore;
                Integer rubOpId = userAnswer.getRubOpId();
                RubricOption rubricOption = optionMapper.selectById(rubOpId);
                if (rubricOption == null){
                    xy = 0;
                }else {
                    Integer rubOpScore = rubricOption.getRubOpScore();
                    xy=xy+rubOpScore;
                }
            }else if(rubType==17){
                QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ques_type ",2);
                queryWrapper.eq("ques_test_type ",17);
                queryWrapper.eq("ques_state ",0);
                Question question = questionMapper.selectOne(queryWrapper);
                float quesScore = question.getQuesScore();
                qiy=quesScore;
                Integer rubOpId = userAnswer.getRubOpId();
                RubricOption rubricOption = optionMapper.selectById(rubOpId);
                if (rubricOption == null){
                    qy = 0;
                }else {
                    Integer rubOpScore = rubricOption.getRubOpScore();
                    qy=qy+rubOpScore;
                }
            }else if(rubType==18){
                QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ques_type ",2);
                queryWrapper.eq("ques_test_type ",18);
                queryWrapper.eq("ques_state ",0);
                Question question = questionMapper.selectOne(queryWrapper);
                float quesScore = question.getQuesScore();
                tep=quesScore;
                Integer rubOpId = userAnswer.getRubOpId();
                RubricOption rubricOption = optionMapper.selectById(rubOpId);
                if (rubricOption == null){
                    tp = 0;
                }else {
                    Integer rubOpScore = rubricOption.getRubOpScore();
                    tp=tp+rubOpScore;
                }
            }
        }
        String mindTestQuti=null;
        String mindTestQiangpozheng=null;
        String mindTestRenji=null;
        String mindTestYouyu=null;
        String mindTestJiaolv=null;
        String mindTestDidui=null;
        String mindTestKongbu=null;
        String mindTestPianzhi=null;
        String mindTestJingshenbing=null;
        if(ph==0){
            mindTestQuti="未检测";
        }else if(ph<peh){
            mindTestQuti="是";
        }else {
            mindTestQuti="否";
        }

        if(qx==0){
            mindTestQiangpozheng="未检测";
        }else if(qx<qix){
            mindTestQiangpozheng="是";
        }else {
            mindTestQiangpozheng="否";
        }

        if(yx==0){
            mindTestRenji="未检测";
        }else if(yx<yanx){
            mindTestRenji="是";
        }else {
            mindTestRenji="否";
        }

        if(yxu==0){
            mindTestYouyu="未检测";
        }else if(yxu<yinxu){
            mindTestYouyu="是";
        }else {
            mindTestYouyu="否";
        }

        if(ts==0){
            mindTestJiaolv="未检测";
        }else if(ts<tans){
            mindTestJiaolv="是";
        }else {
            mindTestJiaolv="否";
        }

        if(sr==0){
            mindTestDidui="未检测";
        }else if(sr<shir){
            mindTestDidui="是";
        }else {
            mindTestDidui="否";
        }

        if(xy==0){
            mindTestKongbu="未检测";
        }else if(xy<xuey){
            mindTestKongbu="是";
        }else {
            mindTestKongbu="否";
        }

        if(qy==0){
            mindTestPianzhi="未检测";
        }else if(qy<qiy){
            mindTestPianzhi="是";
        }else {
            mindTestPianzhi="否";
        }

        if(tp==0){
            mindTestJingshenbing="未检测";
        }else if(tp<tep){
            mindTestJingshenbing="是";
        }else {
            mindTestJingshenbing="否";
        }

        QueryWrapper<MindTest> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_id",memId);
        mindTestMapper.delete(wrapper);
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format( now );
        MindTest mindTest = new MindTest();
        mindTest.setMindTestName(mindTestName);
        mindTest.setMindTestDatetime(time);
        mindTest.setMindTestQuti(mindTestQuti);
        mindTest.setMindTestQiangpozheng(mindTestQiangpozheng);
        mindTest.setMindTestRenji(mindTestRenji);
        mindTest.setMindTestYouyu(mindTestYouyu);
        mindTest.setMindTestJiaolv(mindTestJiaolv);
        mindTest.setMindTestDidui(mindTestDidui);
        mindTest.setMindTestKongbu(mindTestKongbu);
        mindTest.setMindTestPianzhi(mindTestPianzhi);
        mindTest.setMindTestJingshenbing(mindTestJingshenbing);
        mindTest.setMemId(memId);
        mindTestMapper.insert(mindTest);
    }
}
